package com.shop.dingdongshoppingapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.dingdongshoppingapi.dao.FoodsDao;
import com.shop.dingdongshoppingapi.dao.ShopCarDao;
import com.shop.dingdongshoppingapi.entity.Foods;
import com.shop.dingdongshoppingapi.entity.ShopCar;
import com.shop.dingdongshoppingapi.service.intf.FoodsService;
import com.shop.dingdongshoppingapi.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class FoodsServiceImpl implements FoodsService {
    @Resource
    private FoodsDao dao;
    @Resource
    private ShopCarDao carDao;
    @Override
    public R queryFoods(int gid) {
        if (gid > 0) {
            QueryWrapper<Foods> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("gid",gid);
            List<Foods> foods = dao.selectList(queryWrapper);
            if (foods!=null) {
                return R.ok(foods);
            } else {
                return R.fail();
            }
        }
        return R.fail();
    }

    @Override
    public R addCar(int token, int num,int id) {
        if (id > 0) {
            QueryWrapper<Foods> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", id);
            Foods foods = dao.selectOne(queryWrapper);
            if (foods != null) {
                ShopCar shopCar = new ShopCar();
                shopCar.setUid(token);
                shopCar.setGname(foods.getName());
                shopCar.setGprice(foods.getPrice());
                shopCar.setGnum(num);
                shopCar.setGimage(foods.getImg());
                shopCar.setGctime(new Date());
                int insert = carDao.insert(shopCar);
                if (insert > 0) {
                    return R.ok();
                } else {
                    return R.fail();
                }
            }
        }
                    return R.ok();
    }

    @Override
    public R sel(int id) {
        if (id>0){
            QueryWrapper<Foods> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",id);
            Foods foods = dao.selectOne(queryWrapper);
            if (foods!=null){
                return R.ok(foods);
            } else {
                return R.fail();
            }
        }
        return R.fail();
    }
}
